<?php
namespace WebApper\Service;

class Service extends \WebApper\Base {

	/**
	 * The service id
	 *
	 * @var integer
	 */
	var $service_id;

	/**
	 * The service name for the service
	 *
	 * @var string
	 */
	var $service_name;

	/**
	 * The service duration in min
	 *
	 * @var integer
	 */
	var $service_duration;

	/**
	 * The max seat available
	 *
	 * @var integer
	 */
	var $max_seats;

	/**
	 * The min seat available
	 *
	 * @var integer
	 */
	var $min_seats;

	/**
	 * The fill seats before in min
	 *
	 * @var integer
	 */
	var $fill_seats_before;

	/**
	 * The confirm seats before in min
	 *
	 * @var integer
	 */
	var $confirm_seats_before;

	/**
	 *  The confirm seats within in min
	 *
	 * @var integer
	 */
	var $confirm_seats_within;

	/**
	 * The autoconfirm 
	 *
	 * @var integer
	 */
	var $autoconfirm;

	/**
	 * The form_shortcode
	 *
	 * @var string
	 */
	var $form_shortcode;

	/**
	 * The client confirm email
	 *
	 * @var string
	 */
	var $client_confirm_email;
	
	/**
	 * The client autoconfirm email
	 *
	 * @var string
	 */
	var $client_autoconfirm_email;
	
	/**
	 * The client cancelation email
	 *
	 * @var string
	 */
	var $client_cancelation_email;
	
	/**
	 * The agent confirm email
	 *
	 * @var string
	 */
	var $agent_confirm_email;
	
	/**
	 * The agent cancelation email
	 *
	 * @var string
	 */
	var $agent_cancelation_email;
	
	/**
	 * The admin confirm email
	 *
	 * @var string
	 */
	var $admin_confirm_email;
	
	/**
	 * The admin cancelation email
	 *
	 * @var string
	 */
	var $admin_cancelation_email;
	
	/**
	 * The week num (Ex:0-sunday)
	 *
	 * @var integer
	 */
	var $week_num;
	
	/**
	 * The startTime 
	 *
	 * @var integer
	 */
	var $startTime;
	
	/**
	 * The endTime 
	 *
	 * @var integer
	 */
	var $endTime;
	
	/**
	 * The Max Host 
	 *
	 * @var integer
	 */
	var $maxHost;
	
	/**
	 * The capability 
	 *
	 * @var string
	 */
	var $capability;
	
	

	/** Public Methods ********************************************************/

	/**
	 * Constructor
	 *
	 * @param integer $id
	 */
	function __construct( $service_id = 0 ) {
		if ( !empty( $service_id ) ) {
			$this->service_id = $service_id;
			$this->populate();
		}
	}

	/**
	 * Update or insert task details into the database.
	 *
	 * @global wpdb $wpdb WordPress database object
	 * @return bool Success or failure
	 */
	function save() {
		global $webapper, $wpdb;

		// Update
		if ( !empty( $this->service_id ) ) {
			$result = $wpdb->update( 
				$webapper->tables->services, 
				array( 
					'service_name' 				=> $this->service_name,
					'service_duration' 			=> $this->service_duration,
					'max_seats' 				=> $this->max_seats,
					'min_seats' 				=> $this->min_seats,
					'fill_seats_before' 		=> $this->fill_seats_before,
					'confirm_seats_before' 		=> $this->confirm_seats_before,
					'confirm_seats_within' 		=> $this->confirm_seats_within,
					'autoconfirm' 				=> $this->autoconfirm,
					'form_shortcode' 			=> $this->form_shortcode,
					'client_confirm_email' 		=> $this->client_confirm_email,
					'client_autoconfirm_email' 	=> $this->client_autoconfirm_email,
					'client_cancelation_email' 	=> $this->client_cancelation_email,
					'agent_confirm_email' 		=> $this->agent_confirm_email,
					'agent_cancelation_email' 	=> $this->agent_cancelation_email,
					'admin_confirm_email' 		=> $this->admin_confirm_email,
					'admin_cancelation_email' 	=> $this->admin_cancelation_email,
					'max_host' 					=> $this->maxHost,
					'capability' 				=> $this->capability,
				), 
				array( 'service_id' => $this->service_id ), 
				array( '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s'), 
				array( '%d' ) 
			);	

		// Insert
		} else {
			$result = $wpdb->insert( 
				$webapper->tables->services, 
				array( 
					'service_name' 				=> $this->service_name,
					'service_duration' 			=> $this->service_duration,
					'max_seats' 				=> $this->max_seats,
					'min_seats' 				=> $this->min_seats,
					'fill_seats_before' 		=> $this->fill_seats_before,
					'confirm_seats_before' 		=> $this->confirm_seats_before,
					'confirm_seats_within' 		=> $this->confirm_seats_within,
					'autoconfirm' 				=> $this->autoconfirm,
					'form_shortcode' 			=> $this->form_shortcode,
					'client_confirm_email' 		=> $this->client_confirm_email,
					'client_autoconfirm_email' 	=> $this->client_autoconfirm_email,
					'client_cancelation_email' 	=> $this->client_cancelation_email,
					'agent_confirm_email' 		=> $this->agent_confirm_email,
					'agent_cancelation_email' 	=> $this->agent_cancelation_email,
					'admin_confirm_email' 		=> $this->admin_confirm_email,
					'admin_cancelation_email' 	=> $this->admin_cancelation_email,
					'max_host' 					=> $this->maxHost,
					'capability' 				=> $this->capability,
					'status' 					=> 'active',
				), 
				array( '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s')
			);
			$this->service_id = $wpdb->insert_id;
		}	
		if ( !$result )
			return 0;

		return $this->service_id;
	}

    /**
     * Archives a Services
     *
     */
	public function archive() {
		global $webapper, $wpdb;

		$result = $wpdb->update( 
			$webapper->tables->services, 
			array( 
				'status' => 'archived',
			), 
			array( 'service_id' => $this->service_id ), 
			array( '%s' ),
			array( '%d' )
		);	

		if ( !$result )
			return 0;

		return $this->service_id;
	}
	
	public function save_schedule()
	{
		global $webapper, $wpdb;
		$result = $wpdb->insert( 
			$webapper->tables->schedules, 
			array( 
				'service_id'=> $this->service_id,
				'week_num' 	=> $this->week_num,
				'startTime' => $this->startTime,
				'endTime' 	=> $this->endTime,					
			), 
			array( '%d', '%d', '%d', '%d')
		);
		if( !$result )
			return false;
		return true;
	}

	/** Private Methods *******************************************************/

	/**
	 * Fetches the task data from the database.
	 *
	 * @global $webapper
	 * @global wpdb $wpdb WordPress database object
	 */
	private function populate() {
		global $webapper, $wpdb;

		if ( $service = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$webapper->tables->services} WHERE service_id = %d and status=%s", $this->service_id , 'active') ) ) {
			$this->service_id				= $service->service_id;
			$this->service_name				= $service->service_name;
			$this->service_duration			= $service->service_duration;
			$this->max_seats				= $service->max_seats;
			$this->min_seats				= $service->min_seats;
			$this->fill_seats_before		= $service->fill_seats_before;
			$this->confirm_seats_before		= $service->confirm_seats_before;
			$this->confirm_seats_within		= $service->confirm_seats_within;
			$this->autoconfirm				= $service->autoconfirm;
			$this->form_shortcode			= $service->form_shortcode;
			$this->client_confirm_email		= $service->client_confirm_email;
			$this->client_autoconfirm_email	= $service->client_autoconfirm_email;
			$this->client_cancelation_email = $service->client_cancelation_email;
			$this->agent_confirm_email 		= $service->agent_confirm_email;
			$this->agent_cancelation_email 	= $service->agent_cancelation_email;
			$this->admin_confirm_email 		= $service->admin_confirm_email;
			$this->admin_cancelation_email 	= $service->admin_cancelation_email;			
			$this->maxHost 					= $service->max_host;
			$this->capability 				= $service->capability;			
		}
	}
}

?>